
#include <QDateTime>
#include "./MultiPostTable.h"
#include "Ext/FilterQuery.h"
#include "qDebug"

using namespace std;

namespace TCServer
{
    namespace Store
    {
        QList<QVariantMap> MultiPostTable::queryNewer(const QDateTime &time, uint number)
        {
            FilterQuery q;
            q.prepare(R"(

			SELECT `p`.`id`, `p`.`time`, `p`.`type`, `p`.`imageCount`, `p`.`mainImage`, `p`.`audioSource`, `p`.`comment`,
					`u`.`nickname`, `u`.`headUrl`,
					`u`.`province`, `u`.`city`
			FROM `tc`.`multiposts` `p`
			JOIN `users` `u` ON `p`.`userId` = `u`.`id`
			WHERE `p`.`id` in (
				SELECT * from (
					SELECT `id` FROM `tc`.`multiposts`
					WHERE `time` > ? ORDER BY `time` ASC LIMIT ?
					) as `ids`
			);

			)");
            q.addBindValue(time);
            q.addBindValue(number);

            QList<QVariantMap> res;
            q.exec();

            //qDebug().noquote() << q->executedQuery();
            //qDebug() << "time : " << time;
            //qDebug() << "limit : " << number;

            while (q.next())
            {
                res.append(
                {
                    { "postId", q.nextValue().toULongLong() },
                    { "time", q.nextValue().toDateTime() },
                    { "type", q.nextValue().toString() },
                    { "imageCount", q.nextValue().toUInt() },
                    { "image", q.nextValue().toString() },
                    { "audio", q.nextValue().toString() },
                    { "comment", q.nextValue().toString() },
                    { "name", q.nextValue().toString() },
                    { "head", q.nextValue().toString() },
                    { "province", q.nextValue().toUInt() },
                    { "city", q.nextValue().toString() },
                });
            }
            return res;
        }

        QList<QVariantMap> MultiPostTable::queryOlder(const QDateTime &time, uint number)
        {
            FilterQuery q;
            q.prepare(R"(

			SELECT `p`.`id`, `p`.`time`, `p`.`type`, `p`.`imageCount`, `p`.`mainImage`, `p`.`audioSource`, `p`.`comment`,
					`u`.`nickname`, `u`.`headUrl`,
					`u`.`province`, `u`.`city`
			FROM `tc`.`multiposts` `p`
			JOIN `users` `u` ON `p`.`userId` = `u`.`id`
			WHERE `p`.`id` in (
				SELECT * from (
					SELECT `id` FROM `tc`.`multiposts`
					WHERE `time` < ? ORDER BY `time` DESC LIMIT ?
					) as `ids`
			);

			)");
            q.addBindValue(time);
            q.addBindValue(number);

            QList<QVariantMap> res;
            q.exec();

            //qDebug().noquote() << q->executedQuery();
            //qDebug() << "time : " << time;
            //qDebug() << "limit : " << number;

            while (q.next())
            {
                res.append(
                {
                    { "postId", q.nextValue().toULongLong() },
                    { "time", q.nextValue().toDateTime() },
                    { "type", q.nextValue().toString() },
                    { "imageCount", q.nextValue().toUInt() },
                    { "image", q.nextValue().toString() },
                    { "audio", q.nextValue().toString() },
                    { "comment", q.nextValue().toString() },
                    { "name", q.nextValue().toString() },
                    { "head", q.nextValue().toString() },
                    { "province", q.nextValue().toUInt() },
                    { "city", q.nextValue().toString() },
                });
            }
            return res;
        }

        quint64 MultiPostTable::insert(quint64 userId,
            const QString &type,
            const QString &imageCount,
            const QString &mainImage,
            const QString &audioSource,
            const QString &comment)
        {
            FilterQuery q;
            q.prepare(R"(INSERT INTO `tc`.`multiposts`
			(`userId`,
			`type`,
			`imageCount`,
			`mainImage`,
			`audioSource`,
			`comment`)
			VALUES
			(?, ?, ?, ?, ?, ?);

			)");
            q.addBindValue(userId);
            q.addBindValue(type);
            q.addBindValue(imageCount);
            q.addBindValue(mainImage);
            q.addBindValue(audioSource);
            q.addBindValue(comment);
            q.exec();
            return q.lastInsertId().toULongLong();
        }

    }
}

